package Algorithm.business;

import java.util.*;

/**
 * desc : 模拟扑克牌洗牌发牌
 * time : 2020-07-28 23:39
 *
 * @author wwchen
 */
public class Poker {
    public static void main(String[] args) {
        poker();
        String s = " I am a teacher.";
        String[] arr = s.split(" ");
        System.out.println(arr);

    }
    public static void poker1(){
        HashMap<Integer, String> poker = new HashMap<>();
        getPoker(poker);//抽取了造牌方法;
        Set<Integer> pokerList = poker.keySet();//获取map集合中的键,作为后续操作的对象.
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.addAll(pokerList);
        ArrayList<Integer> player1 = new ArrayList<>();
        ArrayList<Integer> player2 = new ArrayList<>();
        ArrayList<Integer> player3 = new ArrayList<>();
        ArrayList<Integer> player4_dipai = new ArrayList<>();
        Collections.shuffle(list1);//打乱54个编号;
        for (int i = 0; i < list1.size(); i++) {//按照实际发牌一人一张的轮流顺序对牌面的编号进行分发,分别存储到三个玩家列表中
            Integer a = list1.get(i);
            if (i >= 51) {
                player4_dipai.add(a);//留取三张作为底牌
            } else if (i % 3 == 0) {
                player1.add(a);
            } else if (i % 3 == 1) {
                player2.add(a);
            } else {
                player3.add(a);
            }
        }
        Collections.sort(player1);//对三个玩家的编号进行排序.
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(player4_dipai);
        ArrayList<Object> pl1 = new ArrayList<>();//创建四个list集合,使用list中的编号到Map集合中获取对应的键值;
        ArrayList<Object> pl2 = new ArrayList<>();
        ArrayList<Object> pl3 = new ArrayList<>();
        ArrayList<Object> pl4 = new ArrayList<>();
        for (Integer o : player1) {
            String sss = poker.get(o);
            pl1.add(sss);
        }
        for (Integer o : player2) {
            String sss = poker.get(o);
            pl2.add(sss);
        }
        for (Integer o : player3) {
            String sss = poker.get(o);
            pl3.add(sss);
        }
        for (Integer o : player4_dipai) {
            String sss = poker.get(o);
            pl4.add(sss);
        }
        System.out.println("地主的牌是:"+pl1);
        System.out.println("农民1的牌是:"+pl2);
        System.out.println("农民2的牌是:"+pl3);
        System.out.println("底牌是:"+pl4);
    }

    private static void getPoker(HashMap<Integer, String> poker) {
        ArrayList<String> colors = new ArrayList<>();
        ArrayList<String> numbers = new ArrayList<>();
        Collections.addAll(colors, "♠", "♥", "♣", "♦");
        Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        Integer index = 0;
        poker.put(index++, "大王");
        poker.put(index++, "小王");
        String ss = "";
        for (String s : numbers) {
            for (String color : colors) {
                ss = color + s;
                poker.put(index, ss);
                index++;
            }
        }
    }

    public static void poker(){
        Stack<String> stack = new Stack<>();
        HashMap<Integer, String> map = new HashMap<>();
        getPoker1(map);
        Set<Integer> idSet = map.keySet();
        ArrayList<Integer> idList = new ArrayList<>(idSet);
        // 洗牌
        Collections.shuffle(idList);
        // 发牌
        ArrayList<Integer> play1 =new ArrayList<>();
        ArrayList<Integer> play2 =new ArrayList<>();
        ArrayList<Integer> play3 =new ArrayList<>();
        ArrayList<Integer> dipai =new ArrayList<>();
        for(int i=0;i<idList.size();i++){
            if (i>=51){
                dipai.add(idList.get(i));
            } else if(i % 3 == 0){
                play1.add(idList.get(i));
            } else if(i % 3 == 1){
                play2.add(idList.get(i));
            } else {
                play3.add(idList.get(i));
            }
        }
//        Collections.sort(play1);
//        Collections.sort(play2);
//        Collections.sort(play3);
//        Collections.sort(dipai);
        ArrayList<Object> p1 = new ArrayList<>();
        ArrayList<Object> p2 = new ArrayList<>();
        ArrayList<Object> p3 = new ArrayList<>();
        ArrayList<Object> dp = new ArrayList<>();
        play1.stream().sorted().forEach(integer -> p1.add(map.get(integer)));
        play2.stream().sorted().forEach(integer -> p2.add(map.get(integer)));
        play3.stream().sorted().forEach(integer -> p3.add(map.get(integer)));
//        for (Integer i:play1){
//            p1.add(map.get(i));
//        }
//        for (Integer i:play2){
//            p2.add(map.get(i));
//        }
//        for (Integer i:play3){
//            p3.add(map.get(i));
//        }
//        for (Integer i:dipai){
//            dp.add(map.get(i));
//        }
        System.out.println("底牌："+dp);
        System.out.println("paly1:"+p1);
        System.out.println("paly2:"+p2);
        System.out.println("paly3:"+p3);
    }

    /**
     * 取的扑克牌
     * @param poker
     */
    public static void getPoker1(HashMap<Integer, String> poker){
        ArrayList<String> list1 = new ArrayList<>(Arrays.asList("♠", "♥", "♣", "♦"));
        ArrayList<String> list2 = new ArrayList<>(Arrays.asList("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"));
        int i = 0;
        poker.put(i++,"大王");
        poker.put(i++,"小王");
        for (String s : list2){
            for (String s1 : list1){
                poker.put(i++, s+s1);
            }

        }
    }
}


